import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public static void main(String[] args) {
        OperatingSystem OS = OperatingSystem.getInstance();
        // 操作系统内核类型如： 386、486、586等x86
        System.out.println("OS.getArch() = " + OS.getArch());
        System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//
        System.out.println("OS.getDataModel() = " + OS.getDataModel());//
        // 系统描述
        System.out.println("OS.getDescription() = " + OS.getDescription());
        System.out.println("OS.getMachine() = " + OS.getMachine());//
        // 操作系统类型
        System.out.println("OS.getName() = " + OS.getName());
        System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//
        // 操作系统的卖主
        System.out.println("OS.getVendor() = " + OS.getVendor());
        // 卖主名称
        System.out
                .println("OS.getVendorCodeName() = " + OS.getVendorCodeName());
        // 操作系统名称
        System.out.println("OS.getVendorName() = " + OS.getVendorName());
        // 操作系统卖主类型
        System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());
        // 操作系统的版本号
        System.out.println("OS.getVersion() = " + OS.getVersion());
        
        
        Sigar sigar = new Sigar();
        try {
            // 主机名称,IP
            String hostName = "";
            String hostIP = "";
            try {
                hostName = sigar.getNetInfo().getHostName();
                hostIP = sigar.getNetInterfaceConfig().getAddress();
            } catch (SigarException se) {
                hostName = "Unknown Host";
                hostIP = "Unknown IP";
            }
            System.out.println(hostName + "-------" + hostIP);

            CpuPerc cpuCerc = sigar.getCpuPerc();
            System.out.println("*****当前CPU使用情况*****");
            System.out.println("总使用率: " + cpuCerc.getCombined() * 100 + "%");
            System.out.println("用户使用率(user): " +
                    cpuCerc.getUser() * 100 + "%");
            System.out.println("系统使用率(sys): " + cpuCerc.getSys() * 100 + "%");
            System.out.println("优先进程占用(nice): " + cpuCerc.getNice() * 100 + "%");
            System.out.println("当前空闲率(idel): "
                    + cpuCerc.getIdle() * 100 + "%");

            Mem mem = sigar.getMem();
            System.out.println("*****当前内存使用情况*****");
            System.out.println("内存总量：" +
                    mem.getTotal() / 1024 / 1024 + "M" + ":100%");
            System.out.println("已使用内存：" + mem.getActualUsed() / 1024 /
                    1024 + "M" + ":" + mem.getUsedPercent());
            System.out.println("剩余内存：" + mem.getActualFree() / 1024 / 1024
                    + "M" + ":" + mem.getFreePercent());

            FileSystem[] fslist = sigar.getFileSystemList();
            System.out.println("*****当前磁盘使用情况*****");
            for (FileSystem fs : fslist) {
                FileSystemUsage fsu = sigar.getFileSystemUsage(fs.getDirName());
                System.out.println("文件系统名称:" + fs.getDevName());
                System.out.println("空间总量：" +
                        sigar.formatSize(fsu.getTotal() * 1024));
                System.out.println("已使用：" + sigar.formatSize((fsu.getTotal() -
                        fsu.getFree()) * 1024));
                System.out.println("剩余空间：" + sigar.formatSize(fsu.getAvail() * 1024));
            }

        } catch (SigarException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void display(Object vo) {
        Method[] srcMethods = vo.getClass().getMethods();
        String name = null;
        for (Method method : srcMethods) {
            if (method.getName().startsWith("get")) {
                Object obj;
                try {
                    obj = method.invoke(vo, null);
                    System.out.println(method.getName().substring(3) + "-----" + obj.toString());
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }
